package com.suanfa.sort.ti;

import com.suanfa.util.ArrayUtils;

/**
 * @author jukg
 * @date 2025/01/06 15:49
 * @description 给定一个整数数组 nums，将数组中的元素向右轮转 k 个位置，其中 k 是非负数
 */
public class Rotate {

    public static void main(String[] args) {
        int[] nums = {1,2,3,4,5,6,7};
        int k = 3;
        rotate(nums, k);
        ArrayUtils.printArray(nums);
    }

    public static void rotate(int[] nums, int k) {

        reverse(nums, 0, nums.length-1);
        reverse(nums, 0, k-1);
        reverse(nums, k, nums.length-1);
    }

    public static void reverse(int[] nums, int start, int end){
        while(start < end){
            int temp = nums[start];
            nums[start++] = nums[end];
            nums[end--] = temp;
        }

    }
}
